<template>
  <NModal :show="state.show" @UpdateShow="close">
    <div ref="modalRef" class="list-dia">
      <MacHeader
        :title="state.title"
        :isFullscreen="isFullscreen"
        :showDesc="false"
        @toggle="toggle"
        @close="close"
      />
      <div class="content">
        <div>
          <NForm inline :model="tableInfo.name" label-placement="left">
            <NFormItem label="标题">
              <NInput
                v-model:value="tableInfo.queryForm.name"
                placeholder="请输入标题"
                clearable
                style="width: 200px"
              />
            </NFormItem>
            <NFormItem>
              <NButton type="primary" @click="queryData">
                <template #icon>
                  <n-icon>
                    <i class="esayIcon esayIcon-sousuo"></i>
                  </n-icon>
                </template>
                <span>搜索</span>
              </NButton>
            </NFormItem>
          </NForm>
        </div>
        <NDataTable
          style="flex: 1"
          flex-height
          :loading="tableInfo.listLoading"
          :columns="columns"
          :data="tableInfo.tableData"
          :pagination="false"
        />
        <div class="pagination">
          <NPagination
            v-model:page="tableInfo.queryForm.pageNo"
            :page-count="tableInfo.total"
            :page-sizes="tableInfo.pageSizes"
            show-quick-jumper
            show-size-picker
            @updatePage="pageChange"
            @updatePageSize="sizeChange"
          />
        </div>
      </div>
    </div>
  </NModal>
  <photo-detail v-if="showState.showPhotoDetail" ref="photoRef" />
</template>
<script setup lang="ts">
import { ref, reactive, defineAsyncComponent, h, nextTick } from 'vue'
import type { Ref } from 'vue'
import {
  DataTableColumns,
  NModal,
  NDataTable,
  NPagination,
  NInput,
  NForm,
  NFormItem,
  NButton,
  NIcon
} from 'naive-ui'
import MacHeader from '@/components/codeBlock/MacHeader'
import { useFullscreen } from '@vueuse/core'
import { useApi } from '@/hooks/useApi'
import photoListApi from '@/service/api/photo/list'
const PhotoDetail = defineAsyncComponent(
  () => import('@/views/photo/list/components/PhotoDetail.vue')
)
const showState = reactive({
  showPhotoDetail: false
})
const photoRef = ref()
const handDetail = (row: any) => {
  if (showState.showPhotoDetail) {
    photoRef.value.open(row.photo_id)
  } else {
    showState.showPhotoDetail = true
    nextTick(() => {
      setTimeout(() => {
        photoRef.value.open(row.photo_id)
      }, 200)
    })
  }
}

const addRef: Ref<any> = ref()
const delRef: Ref<any> = ref()
const { tableInfo, pageChange, sizeChange, queryData } = useApi({
  isRequest: false,
  addRef,
  delRef,
  getData: photoListApi.buyList,
  initForm: {
    userid: ''
  }
})

const columns: DataTableColumns<any> = [
  {
    title: '标题',
    key: 'photo_name',
    align: 'center',
    render(row: any) {
      return h(
        'div',
        {
          class: 'cursor-pointer',
          onClick: () => handDetail(row)
        },
        { default: () => row.photo_name }
      )
    }
  },
  {
    title: '价格',
    key: 'price'
  },
  {
    title: '购买时间',
    key: 'create_at'
  }
]

const state = reactive({
  show: false,
  title: ''
})
const modalRef: Ref<any> = ref()
const {
  isFullscreen,
  toggle
}: { isFullscreen: Ref<boolean>; toggle: () => Promise<void> } =
  useFullscreen(modalRef)
const close = () => {
  state.show = false
}
const open = (row: any): void => {
  state.title = `${row.nickname}[购买列表]`
  tableInfo.queryForm.userid = row.id
  queryData()
  state.show = true
}

defineExpose({
  open
})
</script>
<style scoped lang="scss">
.list-dia {
  width: 850px;
  height: 80vh;
  background: #fff;
  padding: 10px;
  border-radius: 5px;
  display: flex;
  flex-direction: column;
  .content {
    margin-top: 20px;
    flex: 1;
    display: flex;
    flex-direction: column;
  }
  .add-text {
    margin-left: 10px;
  }
  .pb20 {
    padding-bottom: 20px;
  }
  .pagination {
    margin-top: 10px;
    display: flex;
    justify-content: flex-end;
  }
}
</style>
